<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./node_modules/axios/dist/axios.min.js"></script>
    <style>
        body{
            margin: 0;
            padding: 30px;

        }
    </style>
</head>
<body>
    <h1>修改用户</h1>
    <span>用户名</span><input type="text" class="username">
    <span>密码</span><input type="password" class="password">
    <button class="button">提交</button>
    <script>
    // 获取从用户列表页面携带的id值
    console.log(location)//打印发现href属性和search属性中都有id的值，但search字符较少，推荐使用
     console.log(location.search)
     console.log(location.search.replace('?',''))
     console.log(location.search.replace('?','').split('='))
     console.log(location.search.replace('?','').split('=')[1])
     const id = location.search.replace('?','').split('=')[1]//获取id方式一
    //    获取id方式二，使用urlsearchparams来获取
    // const id1 =  URLSearchParams(location.search).get('id')  
console.log(id)

// 发送axios请求，获取要修改的用户数据，进行回显操作
axios.get(`http://localhost:3000/users/${id}`).then(res=>{
    const user = res.data
    // 回显数据
    document.querySelector('.username').value = user.username
    document.querySelector('.password').value = user.password
})


        // 发送put请求,修改用户
        document.querySelector('.button').addEventListener('click',function(){
            const username = document.querySelector('.username').value
            const password = document.querySelector('.password').value
            //  发送请求put请求
            axios({
                method:'put',
                url:`http://localhost:3000/users/${id}`,
                data:{username:username,password:password}
            }).then(res=>{
                location.href='./index.html'
            })
        })
    </script>
</body>
</html>